package com.ny.service.impl;

import com.ny.entity.Device;
import com.ny.mapper.DeviceMapper;
import com.ny.mapper.DeviceRepository;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DeviceServiceImpl implements DeviceService {
    
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceRepository deviceRepository;

    @Override
    public List<Device> findAll() {
        return deviceRepository.findAll();
    }

    @Override
    public List<Device> findAllDevices() {
        return deviceMapper.findAllDevices();
    }
    
    @Override
    public List<Device> findDevicesByName(String deviceName) {
        System.out.println("Service layer searching for devices with name containing: " + deviceName);
        List<Device> devices = deviceMapper.findDevicesByName(deviceName);
        System.out.println("Service layer found " + devices.size() + " devices");
        return devices;
    }
    
    @Override
    public List<Device> findAirConditioners() {
        System.out.println("Service layer searching for air conditioners");
        List<Device> devices = deviceMapper.findAirConditioners();
        System.out.println("Service layer found " + devices.size() + " air conditioners");
        return devices;
    }
    
    @Override
    public List<Device> findDevicesByNameInMongo(String deviceName) {
        System.out.println("Service layer searching for devices in MongoDB with name: " + deviceName);
        List<Device> devices = deviceRepository.findByDeviceName(deviceName);
        System.out.println("Service layer found " + devices.size() + " devices in MongoDB");
        return devices;
    }
    
    @Override
    public List<Device> findDevicesByNameContainingInMongo(String deviceName) {
        System.out.println("Service layer searching for devices in MongoDB containing name: " + deviceName);
        List<Device> devices = deviceRepository.findByDeviceNameContaining(deviceName);
        System.out.println("Service layer found " + devices.size() + " devices in MongoDB");
        return devices;
    }
    
    @Override
    public Optional<Device> findOneDeviceByNameInMongo(String deviceName) {
        System.out.println("Service layer searching for one device in MongoDB with name: " + deviceName);
        Optional<Device> device = deviceRepository.findByDeviceNameIs(deviceName);
        if (device.isPresent()) {
            System.out.println("Service layer found device in MongoDB: " + device.get().getDeviceName());
        } else {
            System.out.println("Service layer did not find device in MongoDB with name: " + deviceName);
        }
        return device;
    }
    
    @Override
    public List<Device> findDevicesByStatus(Integer status) {
        System.out.println("Service layer searching for devices in MongoDB with status: " + status);
        List<Device> devices = deviceRepository.findByStatus(status);
        System.out.println("Service layer found " + devices.size() + " devices in MongoDB with status " + status);
        return devices;
    }
    
    @Override
    public List<Device> findDevicesByStatusAndTimeRange(Integer status, Date startTime, Date endTime) {
        System.out.println("Service layer searching for devices in MongoDB with status: " + status + 
                          " between " + startTime + " and " + endTime);
        
        // 先获取所有设备来检查数据
        List<Device> allDevices = deviceRepository.findAll();
        System.out.println("Total devices in MongoDB: " + allDevices.size());
        
        for (Device device : allDevices) {
            System.out.println("Device: " + device.getDeviceName() + 
                              ", Status: " + device.getStatus() + 
                              ", Created Time: " + device.getCreatedTime());
        }
        
        List<Device> devices = deviceRepository.findByStatusAndCreatedTimeBetween(status, startTime, endTime);
        System.out.println("Service layer found " + devices.size() + " devices in MongoDB");
        return devices;
    }




    @Override
    public List<Device> findDevicesByTimeRange(Date startTime, Date endTime) {
        System.out.println("Service layer searching for devices in MongoDB between " + 
                          startTime + " and " + endTime);
        List<Device> devices = deviceRepository.findByCreatedTimeBetween(startTime, endTime);
        System.out.println("Service layer found " + devices.size() + " devices in MongoDB");
        return devices;
    }



    @Override
    public void deleteDeviceById(Integer deviceId) {
        System.out.println("Service layer deleting device with ID: " + deviceId);
        deviceRepository.deleteByDeviceId(deviceId);
        System.out.println("Service layer deleted device with ID: " + deviceId);
    }




    @Override
    public List<Device> findDevicesById(Integer deviceId) {
        System.out.println("Service layer searching for devices with ID: " + deviceId);
        List<Device> devices = deviceRepository.findByDeviceId(deviceId);
        System.out.println("Service layer found " + devices.size() + " devices with ID " + deviceId);
        return devices;
    }
    
    @Override
    public int addDevice(Device device) {
        return deviceMapper.insertDevice(device);
    }
}